package com.atguigu.yygh.user.controller;

import com.atguigu.yygh.common.R;
import com.atguigu.yygh.common.utils.AuthContextHolder;
import com.atguigu.yygh.model.user.UserInfo;
import com.atguigu.yygh.user.service.UserService;
import com.atguigu.yygh.user.utils.IpUtils;
import com.atguigu.yygh.vo.user.LoginVo;
import com.atguigu.yygh.vo.user.UserAuthVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author zhmwi
 * @Date 2021/7/16
 * @Dersciption
 */
@RestController
@RequestMapping("/api/user/")
@Api("用户中心")
public class UserApiController {
    @Autowired
    private UserService userService;

    @ApiOperation("会员登录")
    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo,
                   HttpServletRequest request){
        loginVo.setIp(IpUtils.getIpAddr(request));
        Map<String,Object> map = userService.login(loginVo);
        return R.ok().data(map);
    }

    @ApiOperation("用户认证接口")
    @PostMapping("auth/userAuth")
    public R userAuth(HttpServletRequest request,
                      @RequestBody UserAuthVo userAuthVo){
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userService.addUserAuth(userId,userAuthVo);
        return R.ok().data("userInfo",userInfo);
    }
    @ApiOperation("获取用户详情")
    @GetMapping("auth/getUserInfo")
    public R getUserInfo(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userService.getById(userId);
        return R.ok().data("userInfo",userInfo);
    }

}
